package org.nlp2rdf.navigator.benchmark;

import org.nlp2rdf.navigator.component.learn.Learn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Claus Stadler
 * Date: Oct 25, 2010
 * Time: 12:16:48 AM
 */
public class LearningTimePhaseReconfigurator
    implements IPhaseReconfigurator
{
    private static final Logger logger = LoggerFactory.getLogger(LearningTimePhaseReconfigurator.class);

    private Learn learner;
    private List<Integer> learningTimes;

    public LearningTimePhaseReconfigurator(Learn learner, List<Integer> learningTimes) {
        this.learner = learner;
        this.learningTimes = learningTimes;
    }

    public void reconfigure(int phaseId)
    {
        Integer oldValue = learner.getMaxExecutionTime();
        Integer newValue = getValueAtIndexOrLast(learningTimes, phaseId);
        if(newValue != null && !newValue.equals(oldValue)) {
            logger.info("Changing maximum learning time from " + oldValue + " to " + newValue);
            learner.setMaxExecutionTime(newValue);
        }
    }

    public static <T> T getValueAtIndexOrLast(List<T> list, int index)
    {
        index = Math.min(list.size() - 1, index);
        return index < 0 ? null : list.get(index);
    }
}
